package java_class_work.week06.serializable;

import java.io.*;

/**
 * @Auther: admin
 * @Date: 2022/10/18 10:23
 * @Description:
 */

public class SerializableDemo01 {
    public static void main(String[] args) {
        String fileName = ".\\src\\java_class_work\\iotest\\writer.txt";
        File file = new File(fileName);
        // 序列化
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            Person gg = new Person("公权", 23);
            // 写入
            oos.writeObject(gg);
            // 刷新
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 反序列化
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
            // 读取到对象
            Person p = (Person) ois.readObject();
            System.out.println(p);
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
